package com.lframework.starter.web.inner.controller.system;

import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.common.utils.CollectionUtil;
import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.components.generator.GenerateCodeFactory;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.inner.bo.system.generate.GetSysGenerateCodeBo;
import com.lframework.starter.web.inner.bo.system.generate.QuerySysGenerateCodeBo;
import com.lframework.starter.web.inner.entity.SysGenerateCode;
import com.lframework.starter.web.inner.service.GenerateCodeService;
import com.lframework.starter.web.inner.service.system.SysGenerateCodeService;
import com.lframework.starter.web.inner.vo.system.generate.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 编号规则 Controller
 *
 * @author zmj
 */
@Api(tags = "编号规则")
@Validated
@RestController
@RequestMapping("/system/generate/code")
public class SysGenerateCodeController extends DefaultBaseController {

    @Autowired
    private SysGenerateCodeService sysGenerateCodeService;

    @Autowired
    private GenerateCodeService generateCodeService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"system:generate-code:manage"})
    @GetMapping("/query")
    public InvokeResult<PageResult<QuerySysGenerateCodeBo>> query(@Valid QuerySysGenerateCodeVo vo) {

        PageResult<SysGenerateCode> pageResult = sysGenerateCodeService.query(getPageIndex(vo),
                getPageSize(vo), vo);

        List<SysGenerateCode> datas = pageResult.getDatas();
        List<QuerySysGenerateCodeBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QuerySysGenerateCodeBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"system:generate-code:manage"})
    @GetMapping
    public InvokeResult<GetSysGenerateCodeBo> get(@NotNull(message = "id不能为空！") Long id) {

        SysGenerateCode data = sysGenerateCodeService.getById(id);
        if (data == null) {
            throw new DefaultClientException("编号规则不存在！");
        }

        GetSysGenerateCodeBo result = new GetSysGenerateCodeBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({"system:generate-code:manage"})
    @PostMapping
    public InvokeResult<Void> create(@Valid CreateSysGenerateCodeVo vo) {

        sysGenerateCodeService.create(vo);

        return InvokeResultBuilder.success();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @HasPermission({"system:generate-code:manage"})
    @PutMapping
    public InvokeResult<Void> update(@Valid UpdateSysGenerateCodeVo vo) {

        sysGenerateCodeService.update(vo);

        sysGenerateCodeService.cleanCacheByKey(vo.getId());

        return InvokeResultBuilder.success();
    }

    /**
     * 根据ID删除
     */
    @ApiOperation("根据ID删除")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"system:generate-code:manage"})
    @DeleteMapping
    public InvokeResult<Void> deleteById(@NotNull(message = "id不能为空！") Integer id) {

        sysGenerateCodeService.deleteById(id);

        sysGenerateCodeService.cleanCacheByKey(id);

        return InvokeResultBuilder.success();
    }

    /**
     * 设置规则
     */
    @ApiOperation("设置规则")
    @HasPermission({"system:generate-code:manage"})
    @PatchMapping
    public InvokeResult<Void> setting(@Valid SettingSysGenerateCodeVo vo) {

        sysGenerateCodeService.setting(vo);

        sysGenerateCodeService.cleanCacheByKey(vo.getId());

        return InvokeResultBuilder.success();
    }

    /**
     * 预览
     */
    @ApiOperation("预览")
    @HasPermission({"system:generate-code:manage"})
    @PostMapping("/preview")
    public InvokeResult<String> preview(@Valid PreviewSysGenerateCodeVo vo) {

        return InvokeResultBuilder.success(GenerateCodeFactory.generateExample(vo.getConfigStr()));
    }
}
